package com.vf.editor.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileUtil {
	
    
    public static void deleteFolder(String path) {
    	File folder=new File(path);
    	if(folder.exists()) {
        	if(folder.isDirectory()) {
                String[] children = folder.list();
                for (int i=0; i<children.length; i++) {
                	deleteFolder(folder.getAbsolutePath()+"/"+children[i]);
                }
                folder.delete();
        	}else {
        		folder.delete();
        	}
    	}

    }
	
	public static void contentToTxt(String filePath, String content) {
		try {
			File file = new File(filePath);
			if (file.exists())
				file.delete();
			file.createNewFile();
			/*
			 * BufferedWriter writer = new BufferedWriter(new BufferedWriter(new
			 * OutputStreamWriter(new FileOutputStream(content),"utf-8")));
			 */
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
			writer.write("\n" + content);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String readFile(String resourcePath) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader bufferReader = null;
		try {
			bufferReader = new BufferedReader(
					new InputStreamReader(FileUtil.class.getResourceAsStream(resourcePath), "utf-8"));
			while ((line = bufferReader.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bufferReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static String readDiskFile(String filePath) {
		StringBuffer sb = new StringBuffer();
		try (FileReader reader = new FileReader(filePath); BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
		) {
			String line;
			// 网友推荐更加简洁的写法
			while ((line = br.readLine()) != null) {
				// 一次读入一行数据
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 创建目录
	 * 
	 * @param filePath
	 */
	public static void createPath(String filePath) {
		File fp = new File(filePath);
		if (!fp.exists()) {
			fp.mkdirs();
		}
	}

	/**
	 * 删除文件或文件夹
	 * 
	 * @param directory
	 */
	public static void delAllFile(File directory) {
		if (!directory.isDirectory()) {
			directory.delete();
		} else {
			File[] files = directory.listFiles();
			// 空文件夹
			if (files.length == 0) {
				directory.delete();
				return;
			}
			// 删除子文件夹和子文件
			for (File file : files) {
				if (file.isDirectory()) {
					delAllFile(file);
				} else {
					file.delete();
				}
			}
			// 删除文件夹本身
			directory.delete();
		}
	}

	public static String lastName(File file) {
		if (file == null)
			return null;
		String filename = file.getName();
		String[] split = filename.split("\\.");
		if (split.length > 1) {
			return split[split.length - 1];
		} else {
			return "";
		}
	}
	public static String getFileNameWithoutSuffix(File file){
		String fileName = file.getName();
		return fileName.substring(0, fileName.lastIndexOf("."));
	}
}
